//角色初始化的模块数据
package role

import (
	"encoding/json"
	"sanguo/cluster"
	"sanguo/node/common/DropPool"
	"sanguo/node/common/GlobalConstData"
	"sanguo/node/common/actionEvent"
	com_attr "sanguo/node/common/attr"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role/achievement"
	role_attr "sanguo/node/node_game/module/role/att"
	"sanguo/node/node_game/module/role/bag"
	"sanguo/node/node_game/module/role/bossZone"
	"sanguo/node/node_game/module/role/drawCard"
	"sanguo/node/node_game/module/role/mission"
	"sanguo/node/node_game/module/role/skill"
	table_RoleLev "sanguo/node/table/excel/table/RoleLevel"
	table_mission "sanguo/node/table/mission"
	cs_message "sanguo/protocol/cs/message"
	ss_message "sanguo/protocol/ss/message"

	"github.com/golang/protobuf/proto"
)

func (this *Role) GetRoleItem() *bag.RoleItem {
	return this.item
}
func (this *Role) InitRoleItem(items map[int32]*bag.Item) {
	this.item = &bag.RoleItem{}
	this.item.Init(this, items)
	this.module["item"] = this.item
}

func (this *Role) GetRoleSoul() *bag.RoleSoul {
	return this.soul
}
func (this *Role) InitRoleSoul(souls map[uint64]*bag.Soul) {
	this.soul = &bag.RoleSoul{}
	this.soul.Init(souls)
	this.module["soul"] = this.soul
}

//武器
func (this *Role) GetRoleWeapon() *bag.RoleWeapon {
	return this.weapon
}
func (this *Role) InitRoleWeapon(weapons map[int32]*bag.Weapon) {
	this.weapon = &bag.RoleWeapon{}
	this.weapon.Init(weapons)
	this.module["weapon"] = this.weapon
}

//星图成就系统
func (this *Role) GetRoleStarMap() *achievement.RoleStarMap {
	return this.starMap
}
func (this *Role) InitRoleStarMap(starMap_ *achievement.StarMap) {
	this.starMap = &achievement.RoleStarMap{}
	this.starMap.Init(starMap_)
	//校验表数据
	this.starMap.CheckStarMap()
	this.module["starMap"] = this.starMap
}

//角色技能
func (this *Role) GetRoleSkill() *skill.RoleSkill {
	return this.skill
}
func (this *Role) InitRoleSkill(skills map[int32]*skill.SkillPair) {
	this.skill = &skill.RoleSkill{}
	this.skill.Init( /*this,*/ skills)
	//校验表数据
	this.skill.CheckRoleSkill()
	this.module["skill"] = this.skill
}

//武器碎片
func (this *Role) GetRoleWeaponShard() *bag.RoleWeaponShard {
	return this.weaponShard
}
func (this *Role) InitRoleWeaponShard(weaponShards map[int32]*bag.WeaponShard) {
	this.weaponShard = &bag.RoleWeaponShard{}
	this.weaponShard.Init(weaponShards)
	this.module["weaponShard"] = this.weaponShard
}

//通用属性
func (this *Role) GetRoleAttr() *role_attr.RoleAttr {
	return this.attr
}
func (this *Role) InitRoleAttr(attrs map[int32]*role_attr.AttrPair) {
	this.attr = &role_attr.RoleAttr{}
	this.attr.Init(this, attrs)
	//验证表属性条数
	this.attr.CheckAttr()
	this.module["attr"] = this.attr
}

//任务
func (this *Role) GetRoleMission() *mission.RoleMission {
	return this.mission
}
func (this *Role) InitRoleMission(missions map[int32]*mission.Mission) {
	this.mission = &mission.RoleMission{}
	this.mission.Init(this, missions)
	this.module["mission"] = this.mission
}

//抽卡
func (this *Role) GetRoleDrawCard() *drawCard.RoleDrawCard {
	return this.drawCard
}
func (this *Role) InitRoleDrawCard(drawCards map[int32]*drawCard.DrawCard) {
	this.drawCard = &drawCard.RoleDrawCard{}
	this.drawCard.Init(drawCards)

	this.module["drawCard"] = this.drawCard
}

//boss副本
func (this *Role) GetRoleBossZone() *bossZone.RoleBossZone {
	return this.bossZone
}
func (this *Role) InitRoleBossZone(bossZones map[int32]*bossZone.BossZone) {
	this.bossZone = &bossZone.RoleBossZone{}
	this.bossZone.Init(bossZones)
	this.module["bossZone"] = this.bossZone
}

/******************************** 模块验证 ***********************************/

func (this *Role) IsRoleModleData() bool {
	if this.attr == nil || this.mission == nil ||
		this.item == nil || this.weapon == nil ||
		this.weaponShard == nil || this.battleAttr == nil ||
		this.skill == nil || this.starMap == nil ||
		this.drawCard == nil {

		return false
	}
	return true
}

func (this *Role) InitRoleAllModuleData(allMap map[string]string) bool {
	allModule := map[string]bool{
		"mission":     false,
		"attr":        false,
		"item":        false,
		"soul":        false,
		"weapon":      false,
		"weaponShard": false,
		"skill":       false,
		"starMap":     false,
		"drawCard":    false,
		"bossZone":    false,
	}
	for k, v := range allMap {
		if k == "mission" {
			var missions map[int32]*mission.Mission
			err := json.Unmarshal(([]byte)(v), &missions)
			if err != nil {
				return false
			}
			allModule["mission"] = true
			this.InitRoleMission(missions)
		}
		if k == "attr" {
			var attrs map[int32]*role_attr.AttrPair
			err := json.Unmarshal(([]byte)(v), &attrs)
			if err != nil {
				return false
			}
			allModule["attr"] = true
			this.InitRoleAttr(attrs)
		}
		if k == "item" {
			var items map[int32]*bag.Item
			err := json.Unmarshal(([]byte)(v), &items)
			if err != nil {
				return false
			}
			allModule["item"] = true
			this.InitRoleItem(items)
		}

		if k == "soul" {
			var souls map[uint64]*bag.Soul
			err := json.Unmarshal(([]byte)(v), &souls)
			if err != nil {
				return false
			}
			allModule["soul"] = true
			this.InitRoleSoul(souls)
		}
		if k == "weapon" {
			var weapons map[int32]*bag.Weapon
			err := json.Unmarshal(([]byte)(v), &weapons)
			if err != nil {
				return false
			}
			allModule["weapon"] = true
			this.InitRoleWeapon(weapons)
		}
		if k == "weaponShard" {
			var weaponShards map[int32]*bag.WeaponShard
			err := json.Unmarshal(([]byte)(v), &weaponShards)
			if err != nil {
				return false
			}
			allModule["weaponShard"] = true
			this.InitRoleWeaponShard(weaponShards)
		}
		if k == "skill" {
			var skillPairs map[int32]*skill.SkillPair
			err := json.Unmarshal(([]byte)(v), &skillPairs)
			if err != nil {
				return false
			}
			allModule["skill"] = true
			this.InitRoleSkill(skillPairs)
		}
		if k == "starMap" {
			var starMap_ *achievement.StarMap
			err := json.Unmarshal(([]byte)(v), &starMap_)
			if err != nil {
				return false
			}
			allModule["starMap"] = true
			this.InitRoleStarMap(starMap_)
		}
		if k == "drawCard" {
			var drawCards map[int32]*drawCard.DrawCard
			err := json.Unmarshal(([]byte)(v), &drawCards)
			if err != nil {
				return false
			}
			allModule["drawCard"] = true
			this.InitRoleDrawCard(drawCards)
		}
		if k == "bossZone" {
			var bossZones map[int32]*bossZone.BossZone
			err := json.Unmarshal(([]byte)(v), &bossZones)
			if err != nil {
				return false
			}
			allModule["bossZone"] = true
			this.InitRoleBossZone(bossZones)
		}
	}

	for k, v := range allModule {
		if v == false {
			switch k {
			case "mission":
				this.NewInitMission()
			case "attr":
				this.NewInitAttr()
			case "item":
				this.NewInitItem()
			case "soul":
				this.NewInitSoul()
			case "weapon":
				this.NewInitWeapon()
			case "weaponShard":
				this.NewInitWeaponShard()
			case "skill":
				this.NewInitSkill()
			case "starMap":
				this.NewInitStarMap()
			case "drawCard":
				this.NewInitDrawCard()
			case "bossZone":
				this.NewInitBossZone()
			default:
				node_game.Infoln("rolemodule default :", k)
			}
			allModule[k] = true
		}
	}

	this.RoleModleDataOK()
	return true
}

//模块数据加载完成
func (this *Role) RoleModleDataOK() {
	//初始化战斗属性
	this.NewInitBattleAttr()
	//计算战斗属性总和
	this.TotalBattleAttr()

}

/************************** 模块初始化,加载配置文件 ***************************/
//通用属性
func (this *Role) NewInitAttr() {
	node_game.Infof("RoleId:%d new roleModule attr  \n", this.GetID())

	//读表 初始等级
	attrs := make(map[int32]*role_attr.AttrPair)
	for i := 0; i < com_attr.AttrMax; i++ {
		attr := &role_attr.AttrPair{
			Idx: int32(i),
		}
		if i == com_attr.Level { //初始等级
			attr.Val = int64(GlobalConstData.Level)
		} else if i == com_attr.UpperExp {
			attr.Val = int64(table_RoleLev.Table_.IDMap[GlobalConstData.Level].Exp)
		} else if i == com_attr.YHBattleGroundEnterTimes {
			attr.Val = int64(GlobalConstData.YHBattleGroundEnterTimes)
		} else if i == com_attr.StoryStage {
			attr.Val = int64(GlobalConstData.MissionStoryStage)
		} else if i == com_attr.ActionPoint {
			attr.Val = int64(GlobalConstData.ActionPointMax)
		} else {
			attr.Val = 0
		}
		attrs[attr.Idx] = attr
	}
	this.InitRoleAttr(attrs)
	this.GetRoleAttr().SetDirtyFlag()
}

func (this *Role) NewInitMission() {
	node_game.Infof("RoleId:%d new roleModule mission  \n", this.GetID())

	missions := make(map[int32]*mission.Mission)
	//初始任务
	missionID := int32(GlobalConstData.MissionID)
	missions[missionID] = mission.FuncNewMission(missionID)
	this.InitRoleMission(missions)
	this.GetRoleMission().SetDirtyFlag()
}

func (this *Role) NewInitItem() {
	node_game.Infof("RoleId:%d new roleModule item  \n", this.GetID())

	items := make(map[int32]*bag.Item)
	this.InitRoleItem(items)
	this.GetRoleItem().SetDirtyFlag()
}

func (this *Role) NewInitSoul() {
	node_game.Infof("RoleId:%d new roleModule soul  \n", this.GetID())
	souls := make(map[uint64]*bag.Soul)
	this.InitRoleSoul(souls)
	this.GetRoleSoul().SetDirtyFlag()
}

func (this *Role) NewInitWeapon() {
	node_game.Infof("RoleId:%d new roleModule weapon  \n", this.GetID())

	weapons := make(map[int32]*bag.Weapon)
	weapons[int32(100001)] = bag.FuncNewWeapon(int32(100001))
	weapons[100001].State = 1
	this.InitRoleWeapon(weapons)
	this.GetRoleWeapon().SetDirtyFlag()

}
func (this *Role) NewInitWeaponShard() {
	node_game.Infof("RoleId:%d new roleModule WeaponShard  \n", this.GetID())
	weaponShards := make(map[int32]*bag.WeaponShard)
	this.InitRoleWeaponShard(weaponShards)
	this.GetRoleWeaponShard().SetDirtyFlag()
}

func (this *Role) NewInitSkill() {
	node_game.Infof("RoleId:%d new roleModule Skill  \n", this.GetID())

	this.InitRoleSkill(skill.FuncNewSkill())
	this.GetRoleSkill().SetDirtyFlag()
}

func (this *Role) NewInitBossZone() {
	node_game.Infof("RoleId:%d new roleModule BossZone  \n", this.GetID())
	bossZones := make(map[int32]*bossZone.BossZone)
	this.InitRoleBossZone(bossZones)
	this.GetRoleBossZone().SetDirtyFlag()
}

func (this *Role) NewInitStarMap() {
	node_game.Infof("RoleId:%d new roleModule StarMap  \n", this.GetID())

	starMap_ := achievement.FuncNewStarMap()
	this.InitRoleStarMap(starMap_)
	this.GetRoleStarMap().SetDirtyFlag()
}

func (this *Role) NewInitDrawCard() {
	node_game.Infof("RoleId:%d new roleModule DrawCard  \n", this.GetID())
	this.InitRoleDrawCard(drawCard.FuncNewDrawCard())
	this.GetRoleDrawCard().SetDirtyFlag()
}

/***************************角色模块数据变动处理***********************************/
//任务变动处理
func (this *Role) OnMissionAccept(mission *mission.Mission) {
	def := table_mission.GetDef(mission.GetMissionId())
	bef_stage, _ := this.GetRoleAttr().GetAttr(com_attr.StoryStage)

	this.GetRoleAttr().SetAttr(com_attr.StoryStage, int64(def.BaseInfo.StoryStage))

	next_stage, _ := this.GetRoleAttr().GetAttr(com_attr.StoryStage)
	if next_stage != bef_stage && this.NodeMap != nil {
		msg := &ss_message.OnStoryStageChange{
			UserID:     proto.String(this.GetUserID()),
			MapType:    proto.Int32(this.GetMapID()),
			StoryStage: proto.Int64(int64(next_stage)),
		}
		cluster.PostMessage(*this.NodeMap, msg)
	}
}

func (this *Role) OnMissionEnd(mission *mission.Mission) {
	//发放任务奖励
	node_game.Infoln(this.user.GetUserID(), "OnMissionEnd", mission.GetMissionId())
	def := table_mission.GetDef(mission.GetMissionId())
	if nil != def {
		awardAction := def.DeliverInfo.ActionEvent
		for _, v := range awardAction {
			actionEvent.DoActionEvent(v.ActionName, this, v.Params)
		}
	}
}

func (this *Role) OnMissionDirty() {
	if this.GetSelectRoleOk() {
		//node_game.Infoln(this.user.GetUserID(), "OnMissionDirty")
		msg := &cs_message.RoleMissionToC{
			Missions: this.mission.PackMissionModify(),
		}
		this.user.Post(msg)
	}
}

//道具奖励处理
func (this *Role) OnAwardByItem(award *DropPool.Award) {
	if this.GetSelectRoleOk() && award != nil {
		//node_game.Infoln(this.user.GetUserID(), "OnAwardByItem")
		this.GiveOutAward(award)
		msg := &cs_message.GiftBagToC{
			Award: DropPool.PackAward(award),
		}
		this.user.Post(msg)
	}
}

//通用属性变更处理
func (this *Role) OnAttrDirty() {
	if this.GetSelectRoleOk() {
		//node_game.Infoln(this.user.GetUserID(), "OnAttrDirty")
		msg := &cs_message.RoleAttrToC{
			Attrs: this.attr.PackAttrModify(),
		}
		this.user.Post(msg)
	}
}

//武器变更处理
func (this *Role) OnWeaponDirty() {
	if this.GetSelectRoleOk() && this.GetRoleWeapon().GetWeaponDirtyLength() != 0 {
		//node_game.Infoln(this.user.GetUserID(), "OnWeaponDirty")
		msg := &cs_message.RoleWeaponToC{
			Weapons: this.weapon.PackWeaponModify(),
		}
		this.user.Post(msg)
	}
}

//道具变更处理
func (this *Role) OnItemDirty() {
	if this.GetSelectRoleOk() && this.GetRoleItem().GetItemDirtyLength() != 0 {
		//node_game.Infoln(this.user.GetUserID(), "OnItemDirty")
		msg := &cs_message.RoleItemToC{
			Items: this.item.PackItemModify(),
		}
		this.user.Post(msg)
	}
}

//武魂变更处理
func (this *Role) OnSoulDirty() {
	if this.GetSelectRoleOk() && this.GetRoleSoul().GetSoulDirtyLength() != 0 {
		//node_game.Infoln(this.user.GetUserID(), "OnSoulDirty")
		msg := &cs_message.RoleSoulToC{
			Souls: this.soul.PackSoulModify(),
		}
		this.user.Post(msg)
	}
}

//武器碎片变更处理
func (this *Role) OnWeaponShardDirty() {
	if this.GetSelectRoleOk() && this.GetRoleWeaponShard().GetWeaponShardDirtyLength() != 0 {
		//node_game.Infoln(this.user.GetUserID(), "OnWeaponShardDirty")
		msg := &cs_message.RoleWeaponShardToC{
			WeaponShards: this.weaponShard.PackWeaponShardModify(),
		}
		this.user.Post(msg)
	}
}

//星图系统变更
func (this *Role) OnStarMapDirty() {
	if this.GetSelectRoleOk() {
		//node_game.Infoln(this.user.GetUserID(), "OnStarMapDirty")
		msg := &cs_message.RoleStarMapToC{
			StarMap: this.GetRoleStarMap().PackAllStarMapModify(),
		}

		this.user.Post(msg)
	}
}

func (this *Role) OnSkillDirty() {
	if this.GetSelectRoleOk() && this.GetRoleSkill().GetDrityLenght() != 0 {
		skillUpdate := &cs_message.RoleSkillToC{
			RoleSkills: this.GetRoleSkill().PackSkillModify(),
		}
		this.user.Post(skillUpdate)
	}
}

func (this *Role) OnBossZoneDirty() {
	if this.GetSelectRoleOk() && this.GetRoleBossZone().GetBossZoneDirtyLength() != 0 {
		//node_game.Infoln(this.user.GetUserID(), "OnBossZoneDirty")
		msg := &cs_message.RoleBossZoneToC{
			BossZones: this.bossZone.PackBossZoneModify(),
		}
		this.user.Post(msg)
	}
}
